<template>
	<div class="content-box">
		<div class="content-item">
			<div class="title">实名认证</div>
			<!-- 实名认证表单 -->
			<el-form ref="ruleFormRef" :rules="rules" :model="identityInfoForm" label-width="88px">
				<el-form-item label="真实姓名" prop="userName">
					<el-input v-model="identityInfoForm.userName" />
				</el-form-item>
				<!-- <el-form-item label="证件类型" prop="type">
					<el-select v-model="identityInfoForm.type" placeholder="请选择">
						<el-option v-for="item in TypeOptions" :key="item.value" :label="item.label" :value="item.value" />
					</el-select>
				</el-form-item> -->
				<el-form-item label="身份证号" prop="idCard">
					<el-input v-model="identityInfoForm.idCard" />
				</el-form-item>
				<!-- 身份证图片start -->
				<!-- <div class="upload-form">
					<div class="front-box">
						<el-image v-if="identityInfoForm.IdCardPhoto" fit="cover" :src="idcardPhoto.front" />
						<el-image v-else :src="cosPrefix + 'user/identity/idcard-front.png'" fit="cover" />
						<div class="img-box">
							<span>上传证件正面照</span>
							<Upload size="small" :limit="1"></Upload>
						</div>
					</div>
					<div class="back-box">
						<el-image v-if="identityInfoForm.IdCardPhoto" fit="cover" :src="idcardPhoto.back" />
						<el-image v-else :src="cosPrefix + 'user/identity/idcard-back.png'" fit="cover" />
						<div class="img-box">
							<span>上传证件反面照</span>
							<el-upload ref="upload" action="" :limit="1" :on-exceed="handleExceed" :auto-upload="false">
								<template #trigger>
									<el-button size="small">点击上传</el-button>
								</template>
							</el-upload>
						</div>
					</div>
				</div> -->
				<!-- 身份证图片end -->

				<!-- 手机号码start -->
				<!-- <el-form-item label="手机号码">
					<div class="cellphone-box">{{ firstCut + "****" + lastCut }}</div>
					{{ userInfo.tel }}
				</el-form-item> -->

				<!-- <el-form-item label="验证码" prop="code">
					<el-input v-model="identityInfoForm.code" :class="btnClass">
						<template #append>
							<el-button @click="sendVerificationCode" :disabled="isDisabled">
								{{ VerificationText }}
							</el-button>
						</template>
					</el-input>
				</el-form-item> -->
			</el-form>
			<!-- 提交 -->
			<div class="submit">
				<el-button color="#C39F63" @click="submit(ruleFormRef)">提交</el-button>
			</div>
		</div>
	</div>
</template>

<script setup lang="ts" name="Identity">
import { ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { useUserStore } from "@/store/user";
import { userPostIdentity } from "@/service/modules/user";
// import { baseGetValidateCode } from "@/service/modules/base";
// import { ISignUpForm } from "@/components/ToolRight/components/SignUp.vue";

// 实名认证
export interface IAuthentication {
	userName: string;
	idCard: string;
}

// 验证码变量
// const isDisabled = ref(false);
// const btnClass = ref("verification-box");
// const VerificationText = ref("获取验证码");
// 手机号码

const { userInfo } = storeToRefs(useUserStore());
console.log(userInfo);

// 发送验证码后的倒计时
// const countDown = (time: number) => {
// 	isDisabled.value = true;
// 	VerificationText.value = "30秒后重新发送";
// 	btnClass.value = "";
// 	let t = time;
// 	const timer = setInterval(() => {
// 		t--;
// 		VerificationText.value = t + "秒后重新发送";
// 		if (t == 0) {
// 			clearInterval(timer);
// 			isDisabled.value = false;
// 			VerificationText.value = "获取验证码";
// 			btnClass.value = "verification-box";
// 		}
// 	}, 1000);
// };
// 发送验证码方法
// const sendVerificationCode = async () => {
// 	const codeInfo: ISignUpForm = { way: "tel", callAreaCode: "+86", target: cellphone.value, code: "", password: "", confirmPassword: "", isAgree: true };
// 	try {
// 		const { message } = await baseGetValidateCode(codeInfo);
// 		ElMessage.success({ message, duration: 1000 });
// 		countDown(60);
// 	} catch (error) {}
// };
// 上传身份证照片
// const upload = ref<UploadInstance>();

// 表单字段及验证
const ruleFormRef = ref<FormInstance>();

const identityInfoForm = ref<IAuthentication>({
	userName: "",
	idCard: ""
});
const rules = ref<FormRules>({
	userName: [{ required: true, message: "请输入真实姓名", trigger: "blur" }],
	idCard: [{ required: true, message: "请输入身份证号", trigger: "blur" }]
});
// 身份证照片
// const idcardPhoto = {
// 	front: "",
// 	back: ""
// };

// 证件类型选择
// const TypeOptions = [
// 	{ label: "身份证", value: "id" },
// 	{ label: "护照", value: "passport" }
// ];
// 提交实名认证
const submit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid, fields) => {
		if (valid) {
			doIdentity();
		} else {
			console.log("error submit!", fields);
		}
	});
};

/**
 * 实名认证
 */
const doIdentity = async () => {
	const { message } = await userPostIdentity(identityInfoForm.value);
	ElMessage.success({ message });
	// 获取 store用户信息方法
	const { actionGetUserInfo } = useUserStore();
	actionGetUserInfo();
};
</script>

<style scoped lang="scss">
.cellphone-box {
	font-size: 14px;
	color: #999999;
}
.upload-form {
	display: flex;
	height: 200px;
	margin-left: 20px;
	.el-image {
		width: 200px;
		height: 126px;
		&:first-child {
			margin-right: 49px;
		}
	}
	.img-box {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		width: 200px;
		height: 88px;
		margin-top: 16px;
		span {
			margin-top: 3px;
			font-size: 14px;
			font-weight: 700;
			color: #bababa;
		}
	}
}
.submit {
	margin-top: 80px;
	margin-bottom: 40px;
	text-align: center;
	.el-button {
		--el-color-black: #ffffff;
	}
}
.content-box {
	width: 100%;
	overflow: hidden;
	background-color: #ffffff;
	.content-item {
		.title {
			display: flex;
			align-items: center;
			padding: 20px;
			font-size: 16px;
			border-bottom: 1px solid #e8e8e8;
		}
	}
}
:deep(.el-form) {
	margin-top: 41px;
	margin-left: 33px;
	.verification-box {
		--el-fill-color-light: #c39f63;
		--el-color-info: #ffffff;
	}
	.el-form-item {
		margin: 0;
		margin-bottom: 22px;
	}
	.el-input {
		width: 380px;
	}
	.el-form-item__label {
		font-weight: 700;
	}
}
</style>
